package me.zhengjie.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.pojo.entity.*;
import me.zhengjie.pojo.vo.ReturnVo;
import me.zhengjie.sqlservice.*;
import me.zhengjie.state.MaterialStatus;
import me.zhengjie.state.TaskType;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/wcs")
@RequiredArgsConstructor
@Slf4j
public class CcdController {

    private final WcsLineBodyPalletService wcsLineBodyPalletService;

    private final WcsThreeFloorOutboundDetailsService wcsThreeFloorOutboundDetailsService;

    private final WcsThreeFloorOutboundService wcsThreeFloorOutboundService;

    private final WcsConfigurationService wcsConfigurationService;

    private final WcsLineBodyPalletHistoryService wcsLineBodyPalletHistoryService;

    @PostMapping("/v2")
    public ReturnVo receiveAlarm(@RequestBody AlarmCodeDto alarmCode){
        log.info("CCD发送请求，有人进入，通知PLC停止:"+alarmCode.getAlarmCode());
        return ReturnVo.OK();
    }


    @GetMapping("/test")
    public void testTable(){
        if (true) {
            //查询被机械臂标记的料箱
            WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails = wcsThreeFloorOutboundDetailsService.selectMechanicalArmLabel(TaskType.ROBOTIC_ARM_MARK.getStatus());
            if (wcsThreeFloorOutboundDetails != null) {
                //将料箱记录到栈板上
                String containerCode = wcsThreeFloorOutboundDetails.getContainerCode();
                String workOrder = wcsThreeFloorOutboundDetails.getWorkOrder();
                boolean b = wcsThreeFloorOutboundDetailsService.updateMechanicalArmLabel(containerCode, TaskType.ROBOTIC_ARM_DEFAULT.getStatus());
                WcsLineBodyPallet wcsLineBodyPallet = wcsLineBodyPalletService.selectWorkOrder(workOrder);
                if (wcsLineBodyPallet != null) {
                    Integer id = wcsThreeFloorOutboundDetails.getId();
                    Integer palletId = wcsLineBodyPallet.getPalletId();
                    Integer lineBodyCode = wcsLineBodyPallet.getLineBodyCode();
                    //栈板表+1
                    Integer palletQuantity;
                    palletQuantity = wcsLineBodyPallet.getPalletQuantity();
                    palletQuantity++;
                    //更新栈板数量
                    boolean b1 = wcsLineBodyPalletService.updatePalletQuantity(palletId, palletQuantity);
                    if (!b1) {
                        throw new RuntimeException("更新栈板数量失败");
                    }
                    //记录当前容器所在栈板
                    boolean b2 = wcsThreeFloorOutboundDetailsService.updatePallerCode(id, palletId);
                    if (!b2) {
                        throw new RuntimeException("3楼出库单明细记录所在栈板失败");
                    }
                    //工单号完成数+1
                    WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                    if (wcsThreeFloorOutbound != null) {
                        Integer completeNumber;
                        completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
                        completeNumber++;
                        boolean b4 = wcsThreeFloorOutboundService.updateComplete(wcsThreeFloorOutbound.getId(), completeNumber);
                        if (!b4) {
                            throw new RuntimeException("3楼出库单记录栈板数+1失败");
                        }
                    }

                }
            }

            //第一段是否允许往下流
            WcsLineBodyPallet wcsLineBodyPallet = wcsLineBodyPalletService.selectPalletId(33);
            if (wcsLineBodyPallet != null) {
                Integer onePlcCount = 1;
                Integer palletQuantity = wcsLineBodyPallet.getPalletQuantity();
                String workOrder = wcsLineBodyPallet.getWorkOrder();
                Integer palletId = wcsLineBodyPallet.getPalletId();
                Integer palletSize = 16;
                WcsConfiguration wcsConfiguration = wcsConfigurationService.selectKey(TaskType.THREE_FLOOR_OUTBOUND_PALLET_QUANTITY.getMessage());
                if (wcsConfiguration != null) {
                    palletSize = Integer.valueOf(wcsConfiguration.getConfigValue());
                }
                if (Objects.equals(palletQuantity, palletSize) && Objects.equals(palletQuantity, onePlcCount)) {
                    //通知plc往前流
                    System.out.println("通知plc往前流");
                    //将栈板移到历史表
                    WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
                    BeanUtils.copyProperties(wcsLineBodyPallet, wcsLineBodyPalletHistory);
                    boolean b1 = wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
                    //删除栈板信息
                    boolean b2 = wcsLineBodyPalletService.deleteId(palletId);
                    //查询出库单都完成了修改状态为1
                    WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                    if (wcsThreeFloorOutbound != null) {
                        int id = wcsThreeFloorOutbound.getId();
                        Integer total = wcsThreeFloorOutbound.getTotal();
                        Integer completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
                        if (Objects.equals(total, completeNumber)) {
                            //修改出库单，机械臂状态
                            wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
                        }
                    }
                }
                else {
                    //未满栈板数量，判断剩余数量+栈板数量能不能凑一版，能不出，不能将所有料箱到栈板上才出
                    WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                    if (wcsThreeFloorOutbound != null) {
                        Integer total = wcsThreeFloorOutbound.getTotal();
                        Integer completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
                        Integer remainingQuantity;
                        remainingQuantity = total - completeNumber;
                        if (remainingQuantity > palletSize || (remainingQuantity < palletSize && remainingQuantity != 0)) {
                            //通知plc不往下流
                            System.out.println("通知plc不往下流");
                        } else if (remainingQuantity == 0) {
                            //通知plc往前流
                            System.out.println("通知plc往前流");
                            //将栈板移到历史表
                            WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
                            BeanUtils.copyProperties(wcsLineBodyPallet, wcsLineBodyPalletHistory);
                            boolean b1 = wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
                            //删除栈板信息
                            boolean b2 = wcsLineBodyPalletService.deleteId(palletId);

                            WcsThreeFloorOutbound threeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                            if (threeFloorOutbound != null) {
                                int id = threeFloorOutbound.getId();
                                Integer totals = threeFloorOutbound.getTotal();
                                Integer completeNumbers = threeFloorOutbound.getCompleteNumber();
                                if (Objects.equals(totals, completeNumbers)) {
                                    //修改出库单，机械臂状态
                                    wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
                                }
                            }

                        }
                    }
                }
                System.out.println("011111111111");
            }
        }
    }
}
